/**
 * 系统服务
 * 处理系统管理相关业务逻辑
 */

import { request } from '@/utils/request'
import { API_ENDPOINTS } from '@/constants'
import type { Role, RoleQuery, RoleForm } from '@/types/system'
import type { Menu, MenuQuery, MenuForm } from '@/types/menu'
import type { Dept, DeptQuery, DeptForm } from '@/types/system'
import type { Settings } from '@/types/settings'

export class SystemService {
  // ========== 角色管理 ==========
  
  /**
   * 获取角色列表
   */
  static async getRoleList(params: RoleQuery) {
    try {
      const response = await request.get(API_ENDPOINTS.ROLES, { params })
      return response.data
    } catch (error) {
      console.error('获取角色列表失败:', error)
      throw error
    }
  }

  /**
   * 获取角色详情
   */
  static async getRoleDetail(id: string): Promise<Role> {
    try {
      const response = await request.get(API_ENDPOINTS.ROLE_DETAIL(id))
      return response.data
    } catch (error) {
      console.error('获取角色详情失败:', error)
      throw error
    }
  }

  /**
   * 创建角色
   */
  static async createRole(roleForm: RoleForm): Promise<Role> {
    try {
      const response = await request.post(API_ENDPOINTS.ROLES, roleForm)
      return response.data
    } catch (error) {
      console.error('创建角色失败:', error)
      throw error
    }
  }

  /**
   * 更新角色
   */
  static async updateRole(id: string, roleForm: RoleForm): Promise<Role> {
    try {
      const response = await request.put(API_ENDPOINTS.ROLE_DETAIL(id), roleForm)
      return response.data
    } catch (error) {
      console.error('更新角色失败:', error)
      throw error
    }
  }

  /**
   * 删除角色
   */
  static async deleteRole(id: string): Promise<void> {
    try {
      await request.delete(API_ENDPOINTS.ROLE_DETAIL(id))
    } catch (error) {
      console.error('删除角色失败:', error)
      throw error
    }
  }

  // ========== 菜单管理 ==========

  /**
   * 获取菜单列表
   */
  static async getMenuList(params?: MenuQuery) {
    try {
      const response = await request.get(API_ENDPOINTS.MENUS, { params })
      return response.data
    } catch (error) {
      console.error('获取菜单列表失败:', error)
      throw error
    }
  }

  /**
   * 获取菜单详情
   */
  static async getMenuDetail(id: string): Promise<Menu> {
    try {
      const response = await request.get(API_ENDPOINTS.MENU_DETAIL(id))
      return response.data
    } catch (error) {
      console.error('获取菜单详情失败:', error)
      throw error
    }
  }

  /**
   * 创建菜单
   */
  static async createMenu(menuForm: MenuForm): Promise<Menu> {
    try {
      const response = await request.post(API_ENDPOINTS.MENUS, menuForm)
      return response.data
    } catch (error) {
      console.error('创建菜单失败:', error)
      throw error
    }
  }

  /**
   * 更新菜单
   */
  static async updateMenu(id: string, menuForm: MenuForm): Promise<Menu> {
    try {
      const response = await request.put(API_ENDPOINTS.MENU_DETAIL(id), menuForm)
      return response.data
    } catch (error) {
      console.error('更新菜单失败:', error)
      throw error
    }
  }

  /**
   * 删除菜单
   */
  static async deleteMenu(id: string): Promise<void> {
    try {
      await request.delete(API_ENDPOINTS.MENU_DETAIL(id))
    } catch (error) {
      console.error('删除菜单失败:', error)
      throw error
    }
  }

  // ========== 部门管理 ==========

  /**
   * 获取部门列表
   */
  static async getDeptList(params?: DeptQuery) {
    try {
      const response = await request.get(API_ENDPOINTS.DEPTS, { params })
      return response.data
    } catch (error) {
      console.error('获取部门列表失败:', error)
      throw error
    }
  }

  /**
   * 获取部门详情
   */
  static async getDeptDetail(id: string): Promise<Dept> {
    try {
      const response = await request.get(API_ENDPOINTS.DEPT_DETAIL(id))
      return response.data
    } catch (error) {
      console.error('获取部门详情失败:', error)
      throw error
    }
  }

  /**
   * 创建部门
   */
  static async createDept(deptForm: DeptForm): Promise<Dept> {
    try {
      const response = await request.post(API_ENDPOINTS.DEPTS, deptForm)
      return response.data
    } catch (error) {
      console.error('创建部门失败:', error)
      throw error
    }
  }

  /**
   * 更新部门
   */
  static async updateDept(id: string, deptForm: DeptForm): Promise<Dept> {
    try {
      const response = await request.put(API_ENDPOINTS.DEPT_DETAIL(id), deptForm)
      return response.data
    } catch (error) {
      console.error('更新部门失败:', error)
      throw error
    }
  }

  /**
   * 删除部门
   */
  static async deleteDept(id: string): Promise<void> {
    try {
      await request.delete(API_ENDPOINTS.DEPT_DETAIL(id))
    } catch (error) {
      console.error('删除部门失败:', error)
      throw error
    }
  }

  // ========== 系统设置 ==========

  /**
   * 获取系统设置
   */
  static async getSettings(): Promise<Settings> {
    try {
      const response = await request.get(API_ENDPOINTS.SETTINGS)
      return response.data
    } catch (error) {
      console.error('获取系统设置失败:', error)
      throw error
    }
  }

  /**
   * 更新系统设置
   */
  static async updateSettings(settings: Partial<Settings>): Promise<Settings> {
    try {
      const response = await request.put(API_ENDPOINTS.SETTINGS, settings)
      return response.data
    } catch (error) {
      console.error('更新系统设置失败:', error)
      throw error
    }
  }
}
